package com.janwen.j2ee.jmx;

import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServerMbeanClient {

	static final Log logger = LogFactory.getLog(ServerMbeanClient.class);
	public static void main(String[] args) throws Exception {
            init();
            manage();
	}
	private static ObjectName objectName ; 
    private static MBeanServer mBeanServer; 
	private static void init() throws Exception {
		ServerImpl serverImpl = new ServerImpl();
		ServerMonitor serverMonitor = new ServerMonitor(serverImpl);
		mBeanServer = MBeanServerFactory.createMBeanServer();
		objectName = new ObjectName("objectName:id=ServerMonitor1");
		mBeanServer.registerMBean(serverMonitor, objectName);
	}

	private static void manage() throws Exception {
		//Uptime第一个字母按照get convention,必须大写,跟普通bean不一样
		Long upTime = (Long) mBeanServer.getAttribute(objectName, "UpTime");
		logger.info(upTime);
		MBeanInfo mbean= mBeanServer.getMBeanInfo(objectName);
		//javax.management.MBeanInfo[description=Information on the management interface of the MBean, attributes=[javax.management.MBeanAttributeInfo[description=Attribute exposed for management, name=UpTime, type=long, read-only, descriptor={}]], constructors=[javax.management.MBeanConstructorInfo[description=Public constructor of the MBean, name=com.qianpin.j2ee.jmx.ServerMonitor, signature=[javax.management.MBeanParameterInfo[description=, name=p1, type=com.qianpin.j2ee.jmx.ServerImpl, descriptor={}]], descriptor={}]], operations=[], notifications=[], descriptor={immutableInfo=true, interfaceClassName=com.qianpin.j2ee.jmx.ServerMonitorMBean, mxbean=false}]
		logger.info(mbean);
	}
}
